# -*- coding: utf-8 -*-

import time
from report import report_sxw
import pooler
from osv import osv
from tools.translate import _
import random
from datetime import datetime
        
class Parser(report_sxw.rml_parse):
        
    def __init__(self, cr, uid, name, context):
        super(Parser, self).__init__(cr, uid, name, context=context)
        self.context = context
        self.localcontext.update({
            'init': self._init,
            'semester': self._get_semester,
            'year': self._get_year,
            'details': self._get_details,
            'dat': self._get_dat,
            'xuat_sac': self._get_xuat_sac,
            'gioi': self._get_gioi,
            'kha': self._get_kha,
            'tb_kha': self._get_tb_kha,
            'tb': self._get_tb,
            'yeu': self._get_yeu,
            'rat_yeu': self._get_rat_yeu,
        })
    
    def _init(self, course):
        self.semester = course.academic_semester.name
        self.year = course.academic_year.name
        
        self.thu_ca = ''
        
        self.details = []
        
        self.dat = ''
        self.xuat_sac = ''
        self.gioi = ''
        self.kha = ''
        self.tb_kha = ''
        self.tb = ''
        self.yeu = ''
        self.rat_yeu = ''
        
        
        stt = 0
        marks_ok = True
        
        if marks_ok:
            self.dat = 0
            self.xuat_sac = 0
            self.gioi = 0
            self.kha = 0
            self.tb_kha = 0
            self.tb = 0
            self.yeu = 0
            self.rat_yeu = 0
        
        for course_student in course.course_students:
            stt += 1
            if not marks_ok:
                detail = [stt,
                          course_student.student_id.code, 
                          course_student.student_id.name,
                          '',
                          '',
                          '',
                          '']
            else:
                mark1 = course_student.mark1
                if int(mark1) == mark1:
                    mark1 = int(mark1)
                mark2 = course_student.mark2
                if int(mark2) == mark2:
                    mark2 = int(mark2)
                mark3 = course_student.mark3
                if int(mark3) == mark3:
                    mark3 = int(mark3)
                avg_mark = course_student.avg_mark
                if int(avg_mark) == avg_mark:
                    avg_mark = int(avg_mark)
                
                if avg_mark >= course.min_mark:
                    self.dat += 1
                if avg_mark >= 9:
                    self.xuat_sac += 1
                elif avg_mark >= 8:
                    self.gioi += 1
                elif avg_mark >= 7:
                    self.kha += 1
                elif avg_mark >= 6:
                    self.tb_kha += 1
                elif avg_mark >= 5:
                    self.tb += 1
                elif avg_mark >= 4:
                    self.yeu += 1
                else:
                    self.rat_yeu += 1
                    
                
                detail = [stt,
                          course_student.student_id.code, 
                          course_student.student_id.name,
                          mark1,
                          mark2,
                          mark3,
                          avg_mark]
            self.details.append(detail)
            
            
    def _get_semester(self):
        return self.semester
    
    def _get_year(self):
        return self.year 
    
    def _get_details(self):
        return self.details
    
    def _get_dat(self):
        return self.dat
    
    def _get_xuat_sac(self):
        return self.xuat_sac
    
    def _get_gioi(self):
        return self.gioi
    
    def _get_kha(self):
        return self.kha
    
    def _get_tb_kha(self):
        return self.tb_kha
    
    def _get_tb(self):
        return self.tb
    
    def _get_yeu(self):
        return self.yeu
    
    def _get_rat_yeu(self):
        return self.rat_yeu
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
